package com.gongpingjialoan.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.gongpingjialoan.R;

/**
 * Created by Administrator on 2016/4/11.
 */
public class CircleView extends View {

    private Resources mResources;


    //文本矩形和最外围的距离大小
    private float textTobigDistance;


    private int width;

    private int padding;

    private float margin;

    //最外围圆圈的画笔
    private Paint drawTextPaint;

    //最内文字的画笔
    private Paint drawInerTextPaint;

    //最外围圆圈的范围
    private RectF textRectF;

    //大圆圈的范围
    private RectF bigRectF;

    //大圆圈的宽度
    private int bigWidth;

    //大圆圈画笔
    private Paint bigPaint;

    //最中间圆圈的范围
    private RectF middleRectF;
    //中间画笔
    private Paint middlePaint;

    //最内层虚线画笔
    private Paint endPaint;
    //最内侧圆圈的范围
    private RectF endRectF;

    //位置图标
    private Bitmap mBitmap;

    private Matrix bitmapMatrix;

    private double currentprice = 45.80, price1 = 33.60, price2 = 35.80, price3 = 52.90, price4 = 55.00;

    private int currentColor, type;

    private double factor;

    private double bitmapX, bitmapY;


    private float distance1;
    private float distance2;
    private float distance3;
    private float distance4;
    private float distance5;
    private float distance6;

    private String text2 = "当前车价";

    private String eval = "公平价估值";

    private boolean isDrawPrice = true;

    //只画最外圈
    public boolean isDrawText = true;

    //滑动的角度
    private float arcSweep = 0;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    //取最大值
    private float getBigDistance(float... floats) {
        float max = floats[0];
        int size = floats.length;
        for (int i = 0; i < size; i++) {
            if (floats[i] > max) {
                max = floats[i];
            }
        }
        return max;
    }


    public void setData(double currentprice, double price1, double price2, double price3, double price4, boolean isDrawPrice) {
        this.currentprice = currentprice;
        this.price1 = price1;
        this.price2 = price2;
        this.price3 = price3;
        this.price4 = price4;
        this.isDrawPrice = isDrawPrice;
        getData();
        // invalidate();
    }


    private void getData() {
        distance1 = drawTextPaint.measureText(price1 + "万");
        distance2 = drawTextPaint.measureText("价格偏低");
        distance3 = drawTextPaint.measureText(price2 + "万");
        distance4 = drawTextPaint.measureText(price3 + "万");
        distance5 = drawTextPaint.measureText("价格偏高");
        distance6 = drawTextPaint.measureText(price4 + "万");
        if (price1 >= 100) {
            textTobigDistance = (float) (1.2 * getBigDistance(distance1, distance2, distance3, distance4, distance5, distance6));
        } else {
            textTobigDistance = (float) (1.3 * getBigDistance(distance1, distance2, distance3, distance4, distance5, distance6));
        }
        if (currentprice <= price2) {
            currentColor = 0xFFF16A64;
            factor = Math.max((currentprice - price1) / (price2 - price1), 0);
            arcSweep = (float) (45 * factor);
            type = 0;
            mBitmap = BitmapFactory.decodeResource(mResources, R.drawable.f1);
        } else if (currentprice > price2 && currentprice < price3) {
            currentColor = 0xFF85CF84;
            factor = (currentprice - price2) / (price3 - price2);
            arcSweep = 47 + (float) (156 * factor);
            type = 1;
            mBitmap = BitmapFactory.decodeResource(mResources, R.drawable.f2);
        } else {
            currentColor = 0xFFF9CB62;
            factor = Math.min((currentprice - price3) / (price4 - price3), 1);
            arcSweep = 47 + 158 + (float) (45 * factor);
            type = 2;
            mBitmap = BitmapFactory.decodeResource(mResources, R.drawable.f3);
        }
    }

    public float getArcSweep() {
        return arcSweep;
    }

    public void setArcSweep(float arcSweep) {
        this.arcSweep = arcSweep;
        invalidate();
    }

    private void init() {
        mResources = getContext().getResources();
        bitmapMatrix = new Matrix();
        drawTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        drawTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_smaller));
        //  screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        margin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, mResources.getDisplayMetrics());
        bigWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, mResources.getDisplayMetrics());
        bigPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bigPaint.setStyle(Paint.Style.STROKE);
        bigPaint.setStrokeWidth(bigWidth);

        drawInerTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);


        middlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        middlePaint.setStyle(Paint.Style.STROKE);
        middlePaint.setStrokeCap(Paint.Cap.ROUND);
        middlePaint.setStrokeWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, mResources.getDisplayMetrics()));

        endPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        PathEffect effects = new DashPathEffect(new float[]{8, 8}, 1);
        endPaint.setPathEffect(effects);
        endPaint.setColor(Color.LTGRAY);
        endPaint.setStyle(Paint.Style.STROKE);
        endPaint.setStrokeWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, mResources.getDisplayMetrics()));

        getData();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        int height = (int) (width - 2 * textTobigDistance);
        //只根据右边距
        padding = getPaddingLeft();
        textRectF = new RectF(padding + bigWidth / 2, padding + bigWidth / 2, padding + width - bigWidth / 2, padding + height - bigWidth / 2);
        bigRectF = new RectF(textRectF.left + textTobigDistance, textRectF.top, textRectF.right - textTobigDistance, textRectF.bottom);
        middleRectF = new RectF(bigRectF.left + margin, bigRectF.top + margin, bigRectF.right - margin, bigRectF.bottom - margin);
        endRectF = new RectF(middleRectF.left + margin, middleRectF.top + margin, middleRectF.right - margin, middleRectF.bottom - margin);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bigPaint.setColor(0xFFF16A64);
        // canvas.drawArc(bigRectF, 145, 60, false, bigPaint);
        canvas.drawArc(bigRectF, 145, 45, false, bigPaint);

        bigPaint.setColor(0xFF85CF84);
        //   canvas.drawArc(bigRectF, 207, 126, false, bigPaint);
        canvas.drawArc(bigRectF, 192, 156, false, bigPaint);

        bigPaint.setColor(0xFFF9CB62);
        //  canvas.drawArc(bigRectF, 335, 60, false, bigPaint);
        canvas.drawArc(bigRectF, 350, 45, false, bigPaint);

        middlePaint.setColor(0xffe6e6e6);
        canvas.drawArc(middleRectF, 145, 250, false, middlePaint);

        if (isDrawPrice) {
            middlePaint.setColor(currentColor);
            if (type == 0) {
                canvas.drawArc(middleRectF, 145, arcSweep == 0 ? 0.1f : arcSweep, false, middlePaint);
            } else if (type == 1) {
                canvas.drawArc(middleRectF, 145, arcSweep, false, middlePaint);
            } else {
                canvas.drawArc(middleRectF, 145, arcSweep, false, middlePaint);
            }
            canvas.drawArc(endRectF, 145, 250, false, endPaint);
            float r = (endRectF.right - endRectF.left) / 2;
            bitmapX = endRectF.centerX() + r * Math.cos((arcSweep + 145) * Math.PI / 180);
            bitmapY = r * Math.sin((arcSweep + 145) * Math.PI / 180) + endRectF.centerY();
            bitmapMatrix.setRotate(arcSweep + 145, (float) mBitmap.getWidth() / 2, (float) mBitmap.getHeight() / 2);
            bitmapMatrix.postTranslate((float) bitmapX - mBitmap.getWidth() / 2, (float) bitmapY - mBitmap.getHeight() / 2);
            canvas.drawBitmap(mBitmap, bitmapMatrix, null);
            bitmapMatrix.reset();
        }


        if (isDrawText) {
            float x, y;
            float text_r = (bigRectF.right - bigRectF.left) / 2;
            drawTextPaint.setColor(0xff333333);
            x = (float) (bigRectF.centerX() + Math.cos(145 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(145 * Math.PI / 180) * text_r);
            canvas.drawText(price1 + "万", x - distance1 * 1.5f, y, drawTextPaint);

            drawTextPaint.setColor(0xFFF16A64);
            x = (float) (bigRectF.centerX() + Math.cos(180 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(180 * Math.PI / 180) * text_r);
            canvas.drawText("价格偏低", x - distance2 * 1.3f, y, drawTextPaint);

            drawTextPaint.setColor(0xff333333);
            x = (float) (bigRectF.centerX() + Math.cos(206 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(206 * Math.PI / 180) * text_r);
            canvas.drawText(price2 + "万", x - distance3 * 1.4f, y, drawTextPaint);


            drawTextPaint.setColor(0xff333333);
            x = (float) (bigRectF.centerX() + Math.cos(334 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(334 * Math.PI / 180) * text_r);
            canvas.drawText(price3 + "万", x + distance4 *0.4f, y, drawTextPaint);


            drawTextPaint.setColor(0xFFF9CB62);
            x = (float) (bigRectF.centerX() + Math.cos(0 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(0 * Math.PI / 180) * text_r);
            canvas.drawText("价格偏高", x + distance5 * 0.3f, y, drawTextPaint);


            drawTextPaint.setColor(0xff333333);
            x = (float) (bigRectF.centerX() + Math.cos(35 * Math.PI / 180) * text_r);
            y = (float) (bigRectF.centerY() + Math.sin(35 * Math.PI / 180) * text_r);
            canvas.drawText(price4 + "万", x + distance6 * 0.5f, y, drawTextPaint);

            if (isDrawPrice) {


                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_small));
                drawInerTextPaint.setColor(currentColor);
                canvas.drawText(text2, endRectF.centerX() - drawInerTextPaint.measureText(text2) * 0.5f, endRectF.centerY() - 30, drawInerTextPaint);


                float height1 = drawInerTextPaint.getTextSize();
                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_smallbig));
                drawInerTextPaint.setColor(currentColor);
                canvas.drawText("¥" + currentprice + "万", endRectF.centerX() - drawInerTextPaint.measureText("¥" + currentprice + "万") * 0.5f, endRectF.centerY() + height1 + drawInerTextPaint.getTextSize() * 0.5f - 10, drawInerTextPaint);

//                float height2 = drawInerTextPaint.getTextSize();
//                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_smallest));
//                drawInerTextPaint.setColor(currentColor);
//                canvas.drawText(text4, endRectF.centerX() - drawInerTextPaint.measureText(text4) * 0.5f, endRectF.centerY() + height1 + height2 + 5, drawInerTextPaint);
            } else {
                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_small));
                drawInerTextPaint.setColor(currentColor);
                canvas.drawText(eval, endRectF.centerX() - drawInerTextPaint.measureText(eval) * 0.5f, endRectF.centerY() - 30, drawInerTextPaint);


                float height1 = drawInerTextPaint.getTextSize();
                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_small));
                drawInerTextPaint.setColor(currentColor);
                canvas.drawText("¥" + currentprice + "万", endRectF.centerX() - drawInerTextPaint.measureText("¥" + currentprice + "万") * 0.5f, endRectF.centerY() + height1 + drawInerTextPaint.getTextSize() * 0.5f - 10, drawInerTextPaint);
//                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_smallest));
//                drawInerTextPaint.setColor(0xff333333);
//                canvas.drawText(text1, endRectF.centerX() - drawInerTextPaint.measureText(text1) * 0.5f, endRectF.centerY() - r * 0.5f, drawInerTextPaint);

//                drawInerTextPaint.setTextSize(mResources.getDimensionPixelSize(R.dimen.text_smallest));
//                drawInerTextPaint.setColor(currentColor);
//                canvas.drawText(text4, endRectF.centerX() - drawInerTextPaint.measureText(text4) * 0.5f, endRectF.centerY(), drawInerTextPaint);
            }
        }


    }


}
